package changceilingcolor003;

import java.util.ArrayList;
import java.util.HashMap;

import toxi.geom.Vec3D;

public class Attraction extends Actors {
	private String m_id;
	private String m_description;
	private int m_R;
	private int m_G;
	private int m_B;
	public int m_Min_x = 0;
	public int m_Max_x = 0;
	public int m_Min_y = 0;
	public int m_Max_y = 0;
	public int m_Min_z = 0;
	public int m_Max_z = 0;
	public int m_Min_r = 0;
	public int m_Max_r = 0;
	
	public int output_area=0;
	public int output_countpeople=0;
	public int output_duration=0;
	public Vec3D output_direction;
	public int countTime=0;
	public int timerfixed=0;
	public int timer=1;
	

	
	
	// private float m_MinDistanceFromConnectAttraction = 0;
	// private float m_MaxDistanceFromConnectAttraction = 0;
	private ArrayList<Actors> m_ConnectedAttractors = new ArrayList<Actors>();

	private boolean m_bLocationChanged = false;
	private boolean m_bIsInLigalLocation = false;
	private boolean m_bVisualAlarmState = false;
	private double m_AlaramRadios = 0;
	private double m_AlaramStrenght = 0;
	private boolean m_bAlarmBecauseIsOtherTooClose = false;
	private ArrayList<String> m_PreferedAttractors = new ArrayList<String>();
	private int MaxNumberOfConnectedAttractors =0; 
	Vec3D m_oldLocation = new Vec3D();
	
	////spring


	Attraction(ChangCeilingColor003 parent, String ID, String description,
			int R, int G, int B, int Radious, int Min_x, int Max_x, int Min_y,
			int Max_y, int Min_z, int Max_z, int Min_r, int Max_r,
			Vec3D _location) {
		super(parent, _location);
		m_R = R;
		m_G = G;
		m_B = B;
		m_active = false;
		m_Min_x = Min_x;
		m_Max_x = Max_x;
		m_Min_y = Min_y;
		m_Max_y = Max_y;
		m_Min_z = Min_z;
		m_Max_z = Max_z;
		m_Min_r = Min_r;
		m_Max_r = Max_r;
		m_oldLocation = GetLocation();

		m_id = ID;
		SelfAttrationList.put("ArtScent", 5.0);
		AttractionRadious = Radious;
		InterestDuration = 50;
		m_description = description;	
		//Creator unit;
		
	}
	
	public void AddAttractorToPreferdList(String attractorId)
	{
		m_PreferedAttractors.add(attractorId);
	}
	
	public void SetMaxNumberOfConnection(int numberOfConnection)
	{
		MaxNumberOfConnectedAttractors = numberOfConnection;
	}

	public void OnChangeLocationEvent() {
		
		
		if(m_oldLocation != GetLocation())
		{
			p5.PausePeople = true;
			m_bLocationChanged = true;
		}
		
			
	}
	
/////////////////////////////
	public void GetUnit() {
		//Creator unit;
		 //Vec3D cen=GetLocation().copy();
		 //float radius=(float)GetAttractionRadious();
		 boolean u_active=m_active;
		 return;
		
			
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	private boolean IsNewAttractionStateLiggle(Attraction oOtherAttraction) {

		if (!oOtherAttraction.m_active)
			return true;

		AttractionInteractionProperties oOtherAttractionProperties = p5.AttractionInteractionPropertiesHolder
				.GetAttractorProperties(m_id, oOtherAttraction.m_id);

		m_AlaramRadios = 0;
		m_AlaramStrenght = 0;

		if (oOtherAttractionProperties == null)
			return true;

		float distance = GetLocation().distanceTo(
				oOtherAttraction.GetLocation());

		if (distance < oOtherAttractionProperties.m_MinDistance) {
			m_bAlarmBecauseIsOtherTooClose = true;
			m_AlaramRadios = oOtherAttractionProperties.m_MinDistance;
			m_AlaramStrenght = Math.abs(distance
					- oOtherAttractionProperties.m_MinDistance);
			return false;
		}

		if (distance > oOtherAttractionProperties.m_MaxDistance) {
			m_bAlarmBecauseIsOtherTooClose = false;
			m_AlaramRadios = oOtherAttractionProperties.m_MaxDistance;
			m_AlaramStrenght = Math.abs(distance
					- oOtherAttractionProperties.m_MaxDistance);
			return false;
		}

		return true;

	}

	public boolean AllowToConnectIfPossible(Attraction otherAttractor)
	{
		
		if(m_ConnectedAttractors.size() < this.MaxNumberOfConnectedAttractors && !m_ConnectedAttractors.contains(otherAttractor) )
		{
			m_ConnectedAttractors.add(otherAttractor);			
			return true;
		}
		return false;
	}
	//
	@Override
	public void update(World w) {
		
		if (!m_active)
			return;
		
		if (m_oldLocation != GetLocation()) {
			
			m_bLocationChanged = false;
			m_bIsInLigalLocation = true;
			m_ConnectedAttractors.clear();
			for (int i = 0; i < p5.alisDst.size(); i++) {
				
				
				Attraction examinedAttractor = (Attraction) p5.alisDst.get(i);
				
				if (p5.alisDst.get(i) == this || !(p5.alisDst.get(i).m_active))
					continue;

				if (!IsNewAttractionStateLiggle(examinedAttractor)) {
					m_bIsInLigalLocation = false;
				} else 
				{
					for(int j=0; j< m_PreferedAttractors.size() ;++j)
					{
						if(examinedAttractor.m_id.equals(m_PreferedAttractors.get(j)) &&
								(m_ConnectedAttractors.size() < this.MaxNumberOfConnectedAttractors  ) &&
								examinedAttractor.AllowToConnectIfPossible(this) )
						{								
							m_ConnectedAttractors.add(examinedAttractor);
							
						}
					}				
				}

			}							
		} else {
			return;
		}
		
		
	}

	@Override
	public void Draw() {

		if (!m_active)
			return;
		Vec3D v = GetLocation();
		p5.stroke(m_R, m_G, m_B);
		Vec3D MyLocation = GetLocation();
		for (int i = 0; i < m_ConnectedAttractors.size(); ++i) {			
			Vec3D OtherLocation = m_ConnectedAttractors.get(i).GetLocation();
			p5.line(MyLocation.x, MyLocation.y, MyLocation.z, OtherLocation.x,
					OtherLocation.y, OtherLocation.z);// /////////////////////////////
		}

		p5.strokeWeight(10);
		p5.point(v.x, v.y, v.z);

		p5.strokeWeight(1);		 
		p5.fill(m_R, m_G, m_B,50);
		p5.ellipse(v.x, v.y, (float)AttractionRadious*2,
		(float)AttractionRadious*2);//////////////////////////////////////////////////////////////////

		if (p5.showDetail) {
			p5.text(m_description, GetLocation().x, GetLocation().y);
		}

		if (!m_bIsInLigalLocation) {
			ShowAlaram();
		} else {
			m_bVisualAlarmState = false;

		}

	}

	private void ShowAlaram() {
		if (!p5.showAlarm)
			return;
		// make sure the object blink every 10 frames
		m_bVisualAlarmState = true;

		if (m_bVisualAlarmState) {

			Vec3D v = GetLocation();

			if (m_bAlarmBecauseIsOtherTooClose) {
				p5.fill(150, 0, 0);
			} else {
				// p5.strokeWeight((float) m_AlaramStrenght);
				p5.stroke(255, 0, 0);
			}

			p5.ellipse(v.x, v.y, (float) m_AlaramRadios * 2,
					(float) m_AlaramRadios * 2);
			p5.noFill();
		}
	}

	@Override
	double GetIntrestDuration() {

		return InterestDuration;
	}

	public String GetId() {
		return m_id;
	}

	public String GetDescription() {
		return m_description;
	}

	// get color
	public int GetColorR() {
		return m_R;
	}

	public int GetColorG() {
		return m_G;
	}

	public int GetColorB() {
		return m_B;
	}
	public int GetArea() {
		return output_area;
	}

	public int GetPeople() {
		return output_countpeople;
	}
	public int GetDuration() {
		return output_duration;
	}
	
	@Override
	public double GetAttractionRadious() {
		return AttractionRadious;
	}

}
